﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;
using CinePlancito.Dominio;

namespace CinePlancito.Controllers
{
    public class SalaController : Controller
    {
        string RestSalas = "http://localhost:15497/Salas.svc/salas";
        JavaScriptSerializer js = new JavaScriptSerializer();
 

        public ActionResult ListaSalas()
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest
                .Create(RestSalas + "/");
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string peliculaJson = reader.ReadToEnd();
            List<Sala> salas = js.Deserialize<List<Sala>>(peliculaJson);
            return View(salas);
        }
        public ActionResult DetallesSala(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestSalas + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Sala sala = js.Deserialize<Sala>(salaJson);
            return View(sala);
        }

        public ActionResult EditarSala(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestSalas + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Sala sala = js.Deserialize<Sala>(salaJson);
            return View(sala);
        }

        [HttpPost]
        public ActionResult EditarSala(string id, FormCollection collection)
        {
            try
            {
                string nombre = (string)collection["Nombre"];


                const string quote = "\"";
                string postdata = "{"
                                + quote + "IdSala" + quote + ":" + quote + id + quote + ","
                                + quote + "Nombre" + quote + ":" + quote + nombre + quote +
                                  "}";
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestSalas);
                req.Method = "PUT";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("ListaSalas");
            }
            catch
            {


                return View();
            }
        }

        public ActionResult RegistrarSala()
        {
            return View();
        }
        //CREAR COMBO CON REST
        [HttpPost]
        public ActionResult RegistrarSala(FormCollection collection)
        {




            try
            {


                if (collection["IdSala"].Trim().Length == 0) ModelState.AddModelError("IdSala", "El ID Debe ser ingresado.");
                if (collection["Nombre"].Trim().Length == 0) ModelState.AddModelError("Nombre", "Debe Ingresar Nombre");




                if (!ModelState.IsValid)
                {
                    Sala sala = null;
                    sala = new Sala()
                    {
                        IdSala = int.Parse(collection["IdSala"]),
                        Nombre = (string)collection["Nombre"],
                    };

                    return View(sala);

                }



                int IdSala = int.Parse(collection["IdSala"]);
                string nombre = (string)collection["Nombre"];


                const string quote = "\"";
                string postdata = "{"
                                + quote + "IdSala" + quote + ":" + quote + IdSala + quote + ","
                                + quote + "Nombre" + quote + ":" + quote + nombre + quote +
                                  "}";

                // CREAR REGISTRO 
                byte[] data = Encoding.UTF8.GetBytes(postdata);
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestSalas);
                req.Method = "POST";
                req.ContentLength = data.Length;
                req.ContentType = "application/json";
                var reqStream = req.GetRequestStream();
                reqStream.Write(data, 0, data.Length);
                var res = (HttpWebResponse)req.GetResponse();
                return RedirectToAction("ListaSalas");
            }
            catch
            {

                return View();
            }
        }
        public ActionResult EliminarSala(string id)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestSalas + "/" + id);
            req.Method = "GET";
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            StreamReader reader = new StreamReader(res.GetResponseStream());
            string salaJson = reader.ReadToEnd();
            Sala sala = js.Deserialize<Sala>(salaJson);
            return View(sala);
        }
        [HttpPost]
        public ActionResult EliminarSala(string id, FormCollection collection)
        {
            try
            {

                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(RestSalas + "/" + id);
                req.Method = "DELETE";
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();
                StreamReader reader = new StreamReader(res.GetResponseStream());
                string salaJson = reader.ReadToEnd();
                JavaScriptSerializer js = new JavaScriptSerializer();
                Sala salaEliminado = js.Deserialize<Sala>(salaJson);
                return RedirectToAction("ListaSalas");
            }
            catch
            {
                return View();
            }
        }


    }
}